import os
import time

from Baidu_Text_transAPI import translateBaidu
def printPath(level,path):
    '''''
    打印一个目录下的所有文件夹和文件
    '''
    # 所有文件夹，第一个字段是次目录的级别
    # dirList = []
    # 所有文件
    fileList = []
    # 返回一个列表，其中包含在目录条目的名称
    files = os.listdir(path)
    # 先添加目录级别
    # dirList.append(str(level))
    for f in files:
        # if(os.path.isdir(path + '/' + f)):
        #     # 排除隐藏文件夹。因为隐藏文件夹过多
        #     if(f[0] == '.'):
        #         pass
        #     else:
        #         # 添加非隐藏文件夹
        #         dirList.append(f)
        if(os.path.isfile(path + '/' + f)):
            # 添加文件
            if f.split('.')[1]=='txt':
                fileList.append(f)
    # 当一个标志使用，文件夹列表第一个级别不打印
    # i_dl = 0
    # for dl in dirList:
    #     if(i_dl == 0):
    #         i_dl = i_dl + 1
    #     else:
    #         # 打印至控制台，不是第一个的目录
    #         print('-' * (int(dirList[0])), dl )
    #         # 打印目录下的所有文件夹和文件，目录级别+1
    #         printPath((int(dirList[0]) + 1), path + '/' + dl)
    f2 = open("alldata.txt", 'w', encoding='utf-8')
    for fl in fileList:
        # 打印文件
        print(fl)
        f = open(path+ '/' + fl,encoding='utf-8')#读取完txt再读txt里面的类容
        # print(f.read())
        enstr=f.read()
        chstr=translateBaidu(enstr)[0]['dst']
        f2.write(chstr + '\n')
        f2.write(enstr + '\n')
        time.sleep(1)
        # 以下三行是逐行读取，跟f2.write(f.read())效果一样
        # alllines = f.readlines()
        # for eachLine in alllines:
        #    f2.write(eachLine)
    f2.close()
    print('文件数',len(fileList))
if __name__ == '__main__':
    printPath(1, 'TFH_Annotated_Dataset')
    # print(translateBaidu('An insulating film substantially free from surface irregularities is RF bias sputtered onto a smooth polycrystalline or micro-roughened surface. Controlled sputtering is performed first at a low reemission coefficient and then, after a substantially continuous layer of insulative amorphous film is deposited over the substrate, increasing the reemission coefficient to a second higher level. A low reemission coefficient is about 0.25 and a high coefficient is about 0.7.'))